require 'formula'

class ImapUw < Formula
  # imap-uw is unmaintained software; the author has passed away and there is
  # no active successor project.
  homepage 'http://www.washington.edu/imap/'
  url 'ftp://ftp.cac.washington.edu/imap/imap-2007f.tar.gz'
  mirror 'http://ftp.ntua.gr/pub/net/mail/imap/imap-2007f.tar.gz'
  sha1 '7a82ebd5aae57a5dede96ac4923b63f850ff4fa7'

  bottle do
    cellar :any
    sha1 "74c958abb4628c752632abf47cfc7b96ab034e8b" => :yosemite
    sha1 "8e635bb16b3053ff8bef24fc8261c572fa48ea91" => :mavericks
    sha1 "a68b0990595cefe59e74b8aab30928ab37cbf6f4" => :mountain_lion
  end

  depends_on "openssl"

  def install
    ENV.j1
    inreplace "Makefile" do |s|
      s.gsub! "SSLINCLUDE=/usr/include/openssl",
              "SSLINCLUDE=#{Formula["openssl"].opt_include}/openssl"
      s.gsub! "SSLLIB=/usr/lib",
              "SSLLIB=#{Formula["openssl"].opt_lib}"
      s.gsub! "-DMAC_OSX_KLUDGE=1", "" if MacOS.version >= :snow_leopard
    end
    inreplace "src/osdep/unix/ssl_unix.c", "#include <x509v3.h>\n#include <ssl.h>",
                                           "#include <ssl.h>\n#include <x509v3.h>"
    system "make", "oxp"

    # email servers:
    sbin.install 'imapd/imapd', 'ipopd/ipop2d', 'ipopd/ipop3d'

    # mail utilities:
    bin.install 'dmail/dmail', 'mailutil/mailutil', 'tmail/tmail'

    # c-client library:
    #   Note: Installing the headers from the root c-client directory is not
    #   possible because they are symlinks and homebrew dutifully copies them
    #   as such. Pulling from within the src dir achieves the desired result.
    doc.install Dir['docs/*']
    lib.install 'c-client/c-client.a' => 'libc-client.a'
    (include + 'imap').install 'c-client/osdep.h', 'c-client/linkage.h'
    (include + 'imap').install Dir['src/c-client/*.h', 'src/osdep/unix/*.h']
  end
end
